<?php
class ControllerDelimanLoainhanvien extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();		
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
	 	$this->load->model("deliman/loainhanvien");
		
		
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{	
    	$this->getForm();
	}
	
	public function update()
	{		
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';
		$this->getForm();
  	}
	
	public function delete() 
	{
		$listloainhanvienid=$this->request->post['delete'];
		if(count($listloainhanvienid))
		{
			foreach($listloainhanvienid as $loainhanvienid)
			{
				$this->model_deliman_loainhanvien->delete($loainhanvienid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/loainhanvien/insert');
		$this->data['delete'] = $this->url->http('deliman/loainhanvien/delete');		
		
		$this->data['datas'] = array();
		$where = "";
		$rows = $this->model_deliman_loainhanvien->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		$this->data['thongbao'] = 0;
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['thongbao']++;
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/loainhanvien/update&loainhanvienid='.$this->data['datas'][$i]['loainhanvienid']);
			$this->data['datas'][$i]['text_edit'] = "Sửa";
			
			$this->data['datas'][$i]['link_phanquyen'] = $this->url->http('deliman/loainhanvien/phanquyen&loainhanvienid='.$this->data['datas'][$i]['loainhanvienid']);
			$this->data['datas'][$i]['text_phanquyen'] = "Phân quyền";
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/loainhanvien_list.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	
	private function getForm()
	{		
		
		if ((isset($this->request->get['loainhanvienid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_loainhanvien->getItem($this->request->get['loainhanvienid']);
    	}
		
		$this->id='content';
		$this->template='deliman/loainhanvien_form.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	public function phanquyen()
	{
		$this->data['item'] = $this->model_deliman_loainhanvien->getItem($this->request->get['loainhanvienid']);
		$this->data['quyens'] = $this->string->referSiteMapToArray($this->data['item']['phanquyen']);
		
		$this->load->model("deliman/hanhdong");
		$this->load->model("deliman/chucnang");
		
		$this->data['chucnangs'] = $this->model_deliman_chucnang->getList();
		foreach($this->data['chucnangs'] as $key => $item)
		{
			$where = " AND chucnangid = '".$item['chucnangid']."'";
			$hanhdongs = $this->model_deliman_hanhdong->getList($where);
			$this->data['chucnangs'][$key]['hanhdongs'] = $hanhdongs;
		}
		
		
		$this->id='content';
		$this->template='deliman/loainhanvien_phanquyen.tpl';
		$this->layout="layout/deliman";
		$this->render();
	}
	
	
	public function save()
	{
		$data = $this->request->post;

		if($this->validateForm($data))
		{
			if($data['loainhanvienid']=="")
			{
				$this->model_deliman_loainhanvien->insert($data);	
			}
			else
			{
				$this->model_deliman_loainhanvien->update($data);	
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		if (trim($data['tenloainhanvien']) == "") 
		{
      		$this->error['tenloainhanvien'] = "Bạn chưa nhập tên loại nhân viên";
    	}
		else
		{
			$where = " AND tenloainhanvien = '".$data['tenloainhanvien']."'";
			$nhanviens = $this->model_deliman_loainhanvien->getList($where);
			if(count($nhanviens) > 0)
			{
				$this->error['tenloainhanvien'] = "Tên loại nhân viên đã tồn tại trong hệ thống";	
			}
		}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	public function savePhanQuyen()
	{
		$data = $this->request->post['phanquyen'];
		$loainhanvienid = $this->request->post['loainhanvienid'];
		
		$phanquyen = $this->string->arrayToString($data);
		$this->model_deliman_loainhanvien->updateCol($loainhanvienid,'phanquyen',$phanquyen);
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	//Cac ham xu ly tren form
	
}
?>